/******************************************************************************************
 * Import
 ******************************************************************************************/

// module
import Taro, { Component } from '@tarojs/taro'

// project
import { CPage, CContent, CHeader, CFooter, CFloat, CAround, CBetween, CCenter, CRow, CWrap, CLine, CHand, CItemX, CItemY, CText, CLabel, CSvg, CImage, CButton, CColumn } from '../../../library/comp';
import { Base64, Gen, Time, Zip, Session, Storage, Toast, Loading, Modal, ActionSheet } from '../../../library/func';
import Icon from '../../../project/style/icon';
import Http from '../../../project/func/http/http'
/******************************************************************************************
 * Export Class
 ******************************************************************************************/

export default class AddressList extends Component {

	/*------------------------------------------------------------------------------------
	 * 初始化
	 *-----------------------------------------------------------------------------------*/
	constructor(props) {
		super(props);

		/**
		 * state
		 */
		this.state = {
			background: '#f60',
			status: 'success',
			// 用户收货地址列表
			list:[],
			// 判断是从订单提交页面还是个人中心页面
			type: 0
		}
	}

	/*------------------------------------------------------------------------------------
	 * 生命周期
	 *-----------------------------------------------------------------------------------*/

	//获取列表信息
	getList = (callbcak) => {

		var pages = getCurrentPages(); // 获取页面栈

		var currPage = pages[pages.length - 1]; // 当前页面
		
		var prevPage = pages[pages.length - 2]; // 上一个页面
		
		console.log(prevPage)


		Http.Loading({
			url: 'take/list.php',
			success: (data)=> {
				console.log('data is', data)
				this.setState({
					list: data,
				})
				if(callbcak){
					callbcak();
				}	
			},
			error: (res)=> {
				// console.log(res);
			},
		});

	}
	
	// didMount
	componentDidMount() {
		// console.log('list==============',this.$router.params.type)
		// 修改type
		this.setState({
			type: this.$router.params.type
		})

		this.getList();

	}

	// DidShow
	componentDidShow(){
		this.getList();
	}
	
	/*------------------------------------------------------------------------------------
	 * 事件处理
	 *-----------------------------------------------------------------------------------*/
	// onClick
	onClick = (id, e)=> {
		// console.log('onClick: ' + id)
    }
    
    // onRedirect
    onRedirect = (url, e)=> {
		
        wx.redirectTo({
            url: url
        })
    }

    // onNavigate
    onNavigate = (url, e)=> {
		wx.navigateTo({
			url: url
		})
	}
	
	// 用户设置默认地址
	setDefault = (id, e) => {
		Http.post({
			url:'take/logic/changeDefault.php',
			data:{
				id: id
			},
			success:()=>{
				this.getList(()=>{
					Toast.success('设置成功！')
				});
				
			}
		})
	}

	// 用户删除地址
	removeAddress = (id, e) => {
		Modal.confirm('确定删除地址吗？', '删除后无法恢复', ()=> {
			Http.post({
				url:'take/logic/delete.php',
				data:{
					id: id
				},
				success:(data)=>{
					this.getList(()=>{
						Toast.success('删除成功！')
					});
				},
				error:(res) => {
					Toast.text(res.msg)
				}
			})
		}, ()=> {
			// 点击取消，不做任何操作
		});
	}

	// 从地址列表页面跳转到订单提交页面
	address = (id, e)=> {
		if(this.state.type == 1){
			this.onNavigate({
				url: '/screen/order/submit?takeid=' + id
			})
		}
	}

	/*------------------------------------------------------------------------------------
	 * 自定义方法
	 *-----------------------------------------------------------------------------------*/
	config = {
        navigationBarTitleText: '收获地址',
        navigationBarBackgroundColor: '#dd2f3c',
        navigationBarTextStyle: '#fff'
    }

	/*------------------------------------------------------------------------------------
	 * 渲染
	 *-----------------------------------------------------------------------------------*/
	render () {
		return (
			<CPage>
				<CContent bg='#f5f5f5' h='1600' mb='100'>
					
					{/* 地址列表条目 */}
					{this.state.list.map(item=>{
						return (
							<CColumn bg='#fff' ma='30' ra='20'>
								<CBetween px='20' pt='40' pb='16' onClick={this.address.bind(this, item.id)} >
									<CHand size='28'>{item.set_name}</CHand>
									<CHand size='28'>{item.set_mobile}</CHand>
								</CBetween>
								<CLine px='20' pt='16' mb='20' style='border-bottom:2rpx solid #f3f3f3'>
									<CHand size='28'>{item.set_address}</CHand>
								</CLine>
								<CAround px='20' py='30'>
									{item.is_defaulted == 1 ? 
										<CLine>
											<CSvg icon={Icon.choose} w='40' h='40'></CSvg>
											<CText size='28' pl='10'>默认地址</CText>
										</CLine>
										:
										<CLine y='center' onClick={this.setDefault.bind(this, item.id)}>
											<CHand w='30' h='30' ra='40' ba='2 solid #696969'></CHand>
											<CText size='28' pl='10'>设为默认</CText>
										</CLine>
									}
									<CItemX>
										<CLine y='center'  onClick={this.onNavigate.bind(this, '/screen/user/address/edit?type='+ this.state.type + '&id='+item.id)}>
											<CSvg icon={Icon.convert} w='36' h='36' mr='10'></CSvg>
											<CText size='28'>编辑</CText>
										</CLine>
										<CLine y='center' x='center'  onClick={this.removeAddress.bind(this, item.id)}>
											<CSvg icon={Icon.convert} w='36' h='36' mr='10'></CSvg>
											<CText size='28'>删除</CText>
										</CLine>
									</CItemX>
								</CAround>
							</CColumn>
						)
					})}
					

				</CContent>

				{/* Footer */}
				<CFooter h='100' bg='#de001c' x='center' y='center'>
					<CLine x='center' onClick={this.onNavigate.bind(this, '/screen/user/address/add?type='+ this.state.type)}>
						<CText color='#fff' size='30'>添加地址</CText>
					</CLine>
				</CFooter>
			</CPage>
		)
	}
}


